package com.codeying.dao;

import com.codeying.entity.*;
import com.codeying.utils.*;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.sql.SQLException;

import java.util.*;
/**
 *  评论Dao，和数据库互动。
 */
public class UserCommentDao extends AbstractDao<UserComment>{

    @Override
    protected Class<UserComment> clazz() {
        return UserComment.class;
    }

    @Override
    public String tbName(){
        return "tb_userComment";
    }

    public static UserCommentDao me = new UserCommentDao();

    /**
     * 将map参数转换成sql的where条件语句。
     */
    @Override
    public String toConditionSql(Map<String, String> paramMap){
        StringBuilder sqlBuilder=new StringBuilder("");//接下来拼接查询条件,比如 and xx=xx and XX < xx
        if(paramMap!=null){
                String id = paramMap.get("id");
            if(MyUtils.isNotEmpty(id)){
                        sqlBuilder.append(" and id like '%").append(id).append("%'");// 使用模糊查询
                    }
                    String userid = paramMap.get("userid");
            if(MyUtils.isNotEmpty(userid)){
                        sqlBuilder.append(" and userid like '%").append(userid).append("%'");// 使用模糊查询
                    }
                    String username = paramMap.get("username");
            if(MyUtils.isNotEmpty(username)){
                        sqlBuilder.append(" and username like '%").append(username).append("%'");// 使用模糊查询
                    }
                    String rolech = paramMap.get("rolech");
            if(MyUtils.isNotEmpty(rolech)){
                        sqlBuilder.append(" and rolech like '%").append(rolech).append("%'");// 使用模糊查询
                    }
                    String content = paramMap.get("content");
            if(MyUtils.isNotEmpty(content)){
                        sqlBuilder.append(" and content like '%").append(content).append("%'");// 使用模糊查询
                    }
                    if(MyUtils.isNotEmpty(paramMap.get("createtimeL"))){
                sqlBuilder.append(" and createtime >= '").append(paramMap.get("createtimeL").replace("T"," ")).append("'");
            }
            if(MyUtils.isNotEmpty(paramMap.get("createtimeR"))){
                sqlBuilder.append(" and createtime <= '").append(paramMap.get("createtimeR").replace("T"," ")).append("'");
            }
                    String ctid = paramMap.get("ctid");
            if(MyUtils.isNotEmpty(ctid)){
                        sqlBuilder.append(" and ctid like '%").append(ctid).append("%'");// 使用模糊查询
                    }
                    String type = paramMap.get("type");
            if(MyUtils.isNotEmpty(type)){
                        sqlBuilder.append(" and type like '%").append(type).append("%'");// 使用模糊查询
                    }
                    String status = paramMap.get("status");
            if(MyUtils.isNotEmpty(status)){
                         sqlBuilder.append(" and status = '").append(status).append("'");
                    }
                //排序
            String orderby = paramMap.get("orderby");
            if(orderby!=null){
                sqlBuilder.append(" order by ");
                String[] orderbys = orderby.split(",");
                for (int i = 0 ; i < orderbys.length ; i++) {
                    String s = orderbys[i];
                    sqlBuilder.append(s);
                    if(i!=orderbys.length-1){
                        sqlBuilder.append(",");
                    }
                }
            }
        }
        return sqlBuilder.toString();
    }

    /**
    * 插入
    */
    @Override
    public int insert(UserComment entity){
        try{
            //构造sql语句
            List<Object> paramList = new ArrayList<>();
            StringBuilder reParamNames = new StringBuilder();
            StringBuilder reParams = new StringBuilder();
            paramList.add(entity.getId());
                if(entity.getUserid() != 0){
                reParamNames.append(",userid");
                reParams.append(",?");
                paramList.add(entity.getUserid());
            }
                if(entity.getUsername() != null){
                reParamNames.append(",username");
                reParams.append(",?");
                paramList.add(entity.getUsername());
            }
                if(entity.getRolech() != null){
                reParamNames.append(",rolech");
                reParams.append(",?");
                paramList.add(entity.getRolech());
            }
                if(entity.getContent() != null){
                reParamNames.append(",content");
                reParams.append(",?");
                paramList.add(entity.getContent());
            }
                if(entity.getCreatetime() != null){
                reParamNames.append(",createtime");
                reParams.append(",?");
                paramList.add(entity.getCreatetime());
            }
                if(entity.getCtid() != 0){
                reParamNames.append(",ctid");
                reParams.append(",?");
                paramList.add(entity.getCtid());
            }
                if(entity.getType() != null){
                reParamNames.append(",type");
                reParams.append(",?");
                paramList.add(entity.getType());
            }
                if(entity.getStatus() != null){
                reParamNames.append(",status");
                reParams.append(",?");
                paramList.add(entity.getStatus());
            }
                QueryRunner qr = new QueryRunner();  //创建QueryRunner类对象
            //不为空的插入
            String sqlBuilder = "INSERT INTO tb_userComment (id"+reParamNames.toString()+")VALUES(?"+reParams.toString()+")";
            log(sqlBuilder.toString());
            return qr.update(JdbcUtils.getConnection(), sqlBuilder, paramList.toArray());  // 执行SQL语句
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("UserCommentDao -> insert(-)报错了");
        }
        return 0;
    }

    /**
    * 根据Id更新
    */
    @Override
    public int updateById(UserComment entity) {
        try{
            QueryRunner qr = new QueryRunner();  //创建QueryRunner类对象
            StringBuilder sqlBuilder = new StringBuilder("update tb_userComment set id = ? ");
            List<Object> paramList = new ArrayList<>();
            paramList.add(entity.getId());
                    if(entity.getUserid() != 0){
                sqlBuilder.append(", userid = ? ");
                paramList.add(entity.getUserid());
            }
                    if(entity.getUsername() != null){
                sqlBuilder.append(", username = ? ");
                paramList.add(entity.getUsername());
            }
                    if(entity.getRolech() != null){
                sqlBuilder.append(", rolech = ? ");
                paramList.add(entity.getRolech());
            }
                    if(entity.getContent() != null){
                sqlBuilder.append(", content = ? ");
                paramList.add(entity.getContent());
            }
                    if(entity.getCreatetime() != null){
                sqlBuilder.append(", createtime = ? ");
                paramList.add(entity.getCreatetime());
            }
                    if(entity.getCtid() != 0){
                sqlBuilder.append(", ctid = ? ");
                paramList.add(entity.getCtid());
            }
                    if(entity.getType() != null){
                sqlBuilder.append(", type = ? ");
                paramList.add(entity.getType());
            }
                    if(entity.getStatus() != null){
                sqlBuilder.append(", status = ? ");
                paramList.add(entity.getStatus());
            }
                //最后拼接id条件
            sqlBuilder.append("where id = ?");
            paramList.add(entity.getId());
            log(sqlBuilder.toString(),paramList);
            return qr.update(JdbcUtils.getConnection(), sqlBuilder.toString(), paramList.toArray());  // 执行SQL语句
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("UserCommentDao -> updateById(-)报错了");
        }
        return 0;
    }

}

